//
//  CCTypePriceViewController.m
//  CwinCar
//
//  Created by dlios on 15/10/19.
//  Copyright © 2015年 dlios. All rights reserved.
//

#import "CCTypePriceViewController.h"
#import "BaseHeader.h"
#import "CCTypePriceCell.h"
#import "CCTypePriceModel.h"
#import "FindMapViewController.h"
#import <BaiduMapAPI/BMapKit.h>
@interface CCTypePriceViewController ()<UITableViewDataSource , UITableViewDelegate>
{
    UIView * _headView ;
    UIImageView * _imageHead ;
   }
@property (nonatomic , strong) UITableView * myTableView ;
@property (nonatomic ,strong) CCTypePriceModel * myModel ;
@property (nonatomic ,strong) NSMutableArray * priceArray ;


@end

@implementation CCTypePriceViewController
-(void)viewWillAppear:(BOOL)animated{
    
    [_myTableView reloadData];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO ;
    [self.view setBackgroundColor:[UIColor greenColor]];
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,mainScreen_Width , 64 * suitableScreen_Width)];
    [_headView setBackgroundColor:[UIColor colorWithRed:0.088 green:0.331 blue:1.000 alpha:1.000]];
    [self.view addSubview:_headView];

    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(mainScreen_Width / 2.542, mainScree_Height / 24.703, mainScreen_Width / 3.75, mainScree_Height / 22.233)];
    [_headView addSubview:titleLab];
    [titleLab setText:@"车型报价"];
    [titleLab setTextColor:[UIColor whiteColor]];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:backButton];
    [backButton setFrame:CGRectMake(mainScreen_Width / 22.058, mainScree_Height / 24.703, mainScreen_Width / 12.5, mainScree_Height / 22.233)];
#pragma mark - heade
    _headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width, 350 * suitableScreen_Height)];
    
    if (_myTableView == nil) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64 * suitableScreen_Width, mainScreen_Width, mainScree_Height  - 64 * suitableScreen_Width )];
    }
    [_myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    _myTableView.delegate = self ;
    
    _myTableView.dataSource = self ;
    _myTableView.tableHeaderView = _headView;
    
    
    [self.view addSubview:_myTableView];
    
    _priceArray = [[NSMutableArray alloc]init];
    [self getData];
    //标题
    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * suitableScreen_Width, 285 * suitableScreen_Width, 365 * suitableScreen_Width, 50 * suitableScreen_Width)];
    [_headView addSubview:titleLabel];
    [titleLabel setText:_model.name];
    
    
}
- (void)backAction:(UIButton *)backBut
{
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)getData{
    
    [AFNblock getdataWithString:[NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.0/cars/specsummary-pm1-s%@-c%@.json",self.model.cid,self.cid] block:^(id block) {
       
        NSMutableDictionary * reDic = [block objectForKey:@"result"];
        NSMutableArray * tempArr = [[NSMutableArray alloc]init];
        
        [tempArr addObject:reDic];
        
        for (NSMutableDictionary * dic in tempArr) {
            _myModel = [CCTypePriceModel modelWithDictionary:dic];
            //logo图片
            _imageHead = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, mainScreen_Width,280 * suitableScreen_Width)];
            
            [_headView addSubview:_imageHead];
            [_imageHead sd_setImageWithURL:[NSURL URLWithString:_myModel.logo]placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
            
            NSArray * paraArr = _myModel.paramlist ;
            
            NSString * fore = [[paraArr objectAtIndex:1]objectForKey:@"value"];
            NSString * autoStr = [[paraArr objectAtIndex:2]objectForKey:@"value"];
            UILabel * typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(200 * suitableScreen_Width, 240 * suitableScreen_Width, 175 * suitableScreen_Width, 40 * suitableScreen_Width)];
            [typeLabel setTextAlignment:NSTextAlignmentRight];
            [typeLabel setTextColor:[UIColor whiteColor]];
            [typeLabel setText:[NSString stringWithFormat:@"%@/%@",fore,autoStr]];
            [_imageHead addSubview:typeLabel];
            NSString * place = [[paraArr objectAtIndex:4]objectForKey:@"name"];
            NSString * priceStr = [[paraArr objectAtIndex:4]objectForKey:@"value"];
            NSInteger len = priceStr.length ;
            UILabel * priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * suitableScreen_Width, 315 * suitableScreen_Width, 200 * suitableScreen_Width, 45 * suitableScreen_Width)];
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 350 * suitableScreen_Width, mainScreen_Width, 3 * suitableScreen_Width)];
            [view setBackgroundColor:[UIColor colorWithRed:243/255. green:243/255. blue:243/255. alpha:1]];
            [_headView addSubview:view];
            [priceLabel setFont:[UIFont systemFontOfSize:14]];
            [_headView addSubview:priceLabel];
            NSMutableAttributedString * mutableString = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@:%@",place,priceStr]];
            [mutableString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(7, len)];
            priceLabel.attributedText = mutableString ;
            if (_myModel.dealerlist.count == 0) {
                
                UILabel * emptyLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 450 * suitableScreen_Width, mainScreen_Width, 50 * suitableScreen_Width)];
                [self.view addSubview:emptyLabel];
                [emptyLabel setText:@"当前没有经销商 !"];
                [emptyLabel setFont:[UIFont systemFontOfSize:20 * suitableScreen_Width]];
                [self.view addSubview:emptyLabel];
                [emptyLabel setTextColor:[UIColor redColor]];
                [emptyLabel setTextAlignment:NSTextAlignmentCenter];
                
            }
            for (NSMutableDictionary * deDic  in _myModel.dealerlist) {
                
                _myModel = [CCTypePriceModel modelWithDictionary:deDic];

                NSString * latStr = _myModel.lat ;
                NSString * lonStr = _myModel.lon ;
                double lat = [latStr doubleValue];
                double lon = [lonStr doubleValue] ;
                    BMKMapPoint point1 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(lat,lon));
                
                    BMKMapPoint point2 = BMKMapPointForCoordinate(CLLocationCoordinate2DMake(self.lat ,self.lon));
                
                   _myModel.distance = BMKMetersBetweenMapPoints(point1,point2);
              
                [_priceArray addObject:_myModel];
                
            }
            
        }
        [_myTableView reloadData];
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _priceArray.count ;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 95 * suitableScreen_Width ;
}

-(UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * cellID = @"cell";
    
    CCTypePriceCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell = [[CCTypePriceCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    if (_priceArray.count!=0) {
        
        CCTypePriceModel * model = [_priceArray objectAtIndex:indexPath.row];
        
        [cell getTypeModel:model];
    }
       return cell ;
}

-(void)frameChange{
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FindMapViewController * map = [[FindMapViewController alloc]init];
    map.myLat = self.lat ;
    map.myLon = self.lon ;
    map.model = [_priceArray objectAtIndex:indexPath.row];
    
    map.hidesBottomBarWhenPushed = YES ;
    
    [self.navigationController pushViewController:map animated:YES];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
